@echo off
rem 切换UTF-8 
chcp 65001 1>nul 2>nul

setlocal 

call %~dp0is_admin.bat zh

if %errorlevel%==1 (
    call %~dp0runas_admin.bat "%~0"
    pause
    goto:eof
)

set act=%~1

if "%act%"=="" (
    set act=install
)

set appSetPath=%~2

set appSetName=green_installer.bat
set isAdminName=is_admin.bat
set runasAdminName=runas_admin.bat
set createShortcutName=create_shortcut.bat
set registToPathName=regist_to_path.bat
set startMenuInstallName=start_menu_installer.bat
set appPathInstallName=app_path_installer.bat
set editFileInstallName=edit_file_installer.bat

rem echo %appSetName%
rem echo "%appSetPath%"
rem echo %USERPROFILE%\bin\%appSetName%
rem echo %~dp0%appSetName%


if "%appSetPath%"=="" (
    rem echo %USERPROFILE%\bin\%appSetName%
    set appSetPath=%USERPROFILE%\bin\%appSetName%
    set appSetFile=%~dp0%appSetName%
)

rem echo appSetPath=%appSetPath%        
rem echo copy %appSetFile% to %appSetPath%
if exist "%appSetPath%" (
    rem echo del %appSetPath%
    del %appSetPath%
)

set isAdminPath=%USERPROFILE%\bin\%isAdminName%
set isAdminFile=%~dp0%isAdminName%
if exist "%isAdminPath%" (
    rem echo del %isAdminPath%
    del %isAdminPath%
)

set runAsAdminPath=%USERPROFILE%\bin\%runasAdminName%
set runAsAdminFile=%~dp0%runasAdminName%
if exist "%runAsAdminPath%" (
    del %runAsAdminPath%
)

set createShortcutPath=%USERPROFILE%\bin\%createShortcutName%
set createShortcutFile=%~dp0%createShortcutName%
if exist "%createShortcutPath%" (
    del %createShortcutPath%
)

set registToPathPath=%USERPROFILE%\bin\%registToPathName%
set registToPathFile=%~dp0%registToPathName%
if exist "%registToPathPath%" (
    del %registToPathPath%
)

set startMenuInstallPath=%USERPROFILE%\bin\%startMenuInstallName%
set startMenuInstallFile=%~dp0%startMenuInstallName%
if exist "%startMenuInstallPath%" (
    del %startMenuInstallPath%
)

set appPathInstallPath=%USERPROFILE%\bin\%appPathInstallName%
set appPathInstallFile=%~dp0%appPathInstallName%
if exist "%appPathInstallPath%" (
    del %appPathInstallPath%
)

set editFileInstallPath=%USERPROFILE%\bin\%editFileInstallName%
set editFileInstallFile=%~dp0%editFileInstallName%
if exist "%editFileInstallPath%" (
    del %editFileInstallPath%
)

xcopy %appSetFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %isAdminFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %runAsAdminFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %createShortcutFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %registToPathFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %startMenuInstallFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %appPathInstallFile% %USERPROFILE%\bin /s /y 1>nul 2>nul
xcopy %editFileInstallFile% %USERPROFILE%\bin /s /y 1>nul 2>nul


echo -----------    脚本文件安装完成    ------------------------------------------------------
echo=

REM 右键设置
set createAppSet=%~5

REM 全局安装，需要管理员权限
REM set appSetContextMenu=HKEY_CLASSES_ROOT\*\shell\GreenInsaller
REM rem set appSetContextMenuCommand=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\command
REM set appSetContextMenuInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a0_install
REM set appSetContextMenuUnInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a0_uninstall
REM set contextStartMenuInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a1_StartMenuInstall
REM set contextStartMenuUnInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a1_StartMenuUnInstall
REM set contextAppPathInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a2_AppPathInstall
REM set contextAppPathUnInstall=HKEY_CLASSES_ROOT\*\shell\GreenInsaller\shell\a2_AppPathUnInstall

REM 安装当前用户层面，不需要管理员权限
set appSetContextMenu=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller
set appSetContextMenuInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a0_install
set appSetContextMenuUnInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a0_uninstall
set contextStartMenuInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a1_StartMenuInstall
set contextStartMenuUnInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a1_StartMenuUnInstall
set contextAppPathInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a2_AppPathInstall
set contextAppPathUnInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a2_AppPathUnInstall
set contextEditFileInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a3_EditFileInstall
set contextEditFileUnInstall=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\GreenInsaller\shell\a3_EditFileUnInstall

reg query "%appSetContextMenu%" 1>nul 2>nul
if %errorlevel%==0 (
    reg delete "%appSetContextMenu%" /f > nul
    rem 应该能级联删除
)

if "%act%"=="install" (
    rem 绿色程序设置
    REG ADD "%appSetContextMenu%" /ve /t REG_SZ > nul
    REG ADD "%appSetContextMenu%" /v MUIVerb /t REG_SZ /d "绿色安装" > nul
    REG ADD "%appSetContextMenu%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\cmd.exe,0" > nul
    REG ADD "%appSetContextMenu%" /v AppliesTo /t REG_SZ /d "(System.ItemType:exe) OR (System.ItemType:bat)" > nul
    REG ADD "%appSetContextMenu%" /v SubCommands /t REG_SZ > nul
    rem 安装
    REG ADD "%appSetContextMenuInstall%" /ve /t REG_SZ > nul
    REG ADD "%appSetContextMenuInstall%" /v MUIVerb /t REG_SZ /d "安装" > nul
    REG ADD "%appSetContextMenuInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\cmd.exe,0" > nul
    REM REG ADD "%appSetContextMenuInstall%" /v HasLUAShield /t REG_SZ > nul
    REM REG ADD "%appSetContextMenuInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%runAsAdminPath% %appSetPath% ""%%V"" install\"" > nul
    REM 全局安装才需要管理员权限
    REG ADD "%appSetContextMenuInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%appSetPath% ""%%V"" install\"" > nul
    rem 卸载
    REG ADD "%appSetContextMenuUnInstall%" /ve /t REG_SZ > nul
    REG ADD "%appSetContextMenuUnInstall%" /v MUIVerb /t REG_SZ /d "卸载" > nul
    REG ADD "%appSetContextMenuUnInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,131" > nul
    REM REG ADD "%appSetContextMenuUnInstall%" /v HasLUAShield /t REG_SZ > nul
    REM REG ADD "%appSetContextMenuUnInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%runAsAdminPath% %appSetPath% ""%%V"" uninstall\"" > nul
    REM 全局安装才需要管理员权限
    REG ADD "%appSetContextMenuUnInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%appSetPath% ""%%V"" uninstall\"" > nul
    
    rem ---------------------------------------------------------------------------------------------------------------------------------
    rem 开始菜单快捷方式 开始
    
    rem 创建开始菜单快捷方式
    REG ADD "%contextStartMenuInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextStartMenuInstall%" /v MUIVerb /t REG_SZ /d "创建开始菜单快捷方式" > nul
    REM REG ADD "%contextStartMenuInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextStartMenuInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,22" > nul
    REG ADD "%contextStartMenuInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%startMenuInstallPath% ""%%V"" install\"" > nul
    
    rem 删除开始菜单快捷方式
    REG ADD "%contextStartMenuUnInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextStartMenuUnInstall%" /v MUIVerb /t REG_SZ /d "删除开始菜单快捷方式" > nul
    REM REG ADD "%contextStartMenuUnInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextStartMenuUnInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,131" > nul
    REG ADD "%contextStartMenuUnInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%startMenuInstallPath% ""%%V"" uninstall\"" > nul
    
    rem 开始菜单快捷方式 结束
    rem ---------------------------------------------------------------------------------------------------------------------------------
    
    rem ---------------------------------------------------------------------------------------------------------------------------------
    rem 应用程序索引 开始
    
    rem 添加应用程序索引
    REG ADD "%contextAppPathInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextAppPathInstall%" /v MUIVerb /t REG_SZ /d "添加应用程序索引" > nul
    REM REG ADD "%contextAppPathInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextAppPathInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,24" > nul
    REG ADD "%contextAppPathInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%appPathInstallPath% ""%%V"" install\"" > nul
    
    rem 删除应用程序索引
    REG ADD "%contextAppPathUnInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextAppPathUnInstall%" /v MUIVerb /t REG_SZ /d "删除应用程序索引" > nul
    REM REG ADD "%contextStartMenuUnInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextAppPathUnInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,131" > nul
    REG ADD "%contextAppPathUnInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%appPathInstallPath% ""%%V"" uninstall\"" > nul
    
    rem 开始菜单快捷方式 结束
    rem ---------------------------------------------------------------------------------------------------------------------------------

    rem ---------------------------------------------------------------------------------------------------------------------------------
    rem 右键右键打开 开始

    rem 添加右键打开文件
    REG ADD "%contextEditFileInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextEditFileInstall%" /v MUIVerb /t REG_SZ /d "添加右键打开文件" > nul
    REM REG ADD "%contextEditFileInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextEditFileInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,1" > nul
    REG ADD "%contextEditFileInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%editFileInstallPath% ""%%V"" install\"" > nul

    rem 删除右键打开文件
    REG ADD "%contextEditFileUnInstall%" /ve /t REG_SZ > nul
    REG ADD "%contextEditFileUnInstall%" /v MUIVerb /t REG_SZ /d "删除右键打开文件" > nul
    REM REG ADD "%contextStartMenuUnInstall%" /v HasLUAShield /t REG_SZ > nul
    REG ADD "%contextEditFileUnInstall%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\shell32.dll,131" > nul
    REG ADD "%contextEditFileUnInstall%\command" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /c \"%editFileInstallPath% ""%%V"" uninstall\"" > nul

    rem 右键右键打开 结束
    rem ---------------------------------------------------------------------------------------------------------------------------------
    
    
    rem REG ADD "%appSetContextMenu%" /ve /t REG_SZ /d "绿色程序设置"
    rem REG ADD "%appSetContextMenu%" /v Icon /t REG_SZ /d "%SystemRoot%\System32\cmd.exe,0"
    rem REG ADD "%appSetContextMenu%" /v HasLUAShield /t REG_SZ
    rem REG ADD "%appSetContextMenuCommand%" /ve /t REG_SZ /d "%SystemRoot%\System32\cmd.exe /k \"%runAsAdminPath% %appSetPath% %%V\""
    
    echo -----------    注册表修改完成      ------------------------------------------------------
    echo=
    
    echo -----------    安装完成            ------------------------------------------------------
)

if "%act%"=="uninstall" (
    echo -----------    卸载完成            ------------------------------------------------------
)

pause
